package com.ym.medical.service.impl;

import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.medical.dao.CommentRecordDao;
import com.ym.medical.domain.entity.CommentRecordEntity;
import com.ym.medical.domain.entity.LabelsEntity;
import com.ym.medical.domain.entity.UserTaglibraryEntity;
import com.ym.medical.domain.param.CommentRecordQueryParam;
import com.ym.medical.domain.param.CommentRecordUpdateParam;
import com.ym.medical.domain.param.EvalDoctorParam;
import com.ym.medical.domain.vo.CommentRecordListPcVO;
import com.ym.medical.domain.vo.HotPointVo;
import com.ym.medical.domain.vo.PatientRateTagVo;
import com.ym.medical.domain.vo.PhoneOrderCommentRecord;
import com.ym.medical.service.ICommentRecordSV;
import com.ym.medical.service.IUserTaglibrarySV;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 评价记录Service业务层处理
 *
 * @author ym
 * @date 2020-08-20
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CommentRecordSVImpl extends BaseSVImpl<CommentRecordEntity, CommentRecordDao> implements ICommentRecordSV {

    /**
     * 查询评价记录列表
     *
     * @param queryParam 评价记录 查询参数
     * @return 评价记录
     */
    @Override
    public List<CommentRecordListPcVO> selectListPC(CommentRecordQueryParam queryParam) {
        List<CommentRecordListPcVO> commentRecords = mapper.selectListPC(queryParam);
        return commentRecords;
    }

    /**
     * 新增评价记录
     *
     * @param updateParam 评价记录 新增参数
     * @return 结果
     */
    @Override
    public Long insertPC(CommentRecordUpdateParam updateParam) {
        CommentRecordEntity entity = updateParam.covert();
        this.insert(entity);
        return entity.getId();
    }

    /**
     * 修改评价记录
     *
     * @param updateParam 评价记录 修改参数
     * @return 结果
     */
    @Override
    public int updateByIdPC(CommentRecordUpdateParam updateParam) {
        CommentRecordEntity entity = updateParam.covert();
        int result = this.updateById(entity);
        return result;
    }

    @Autowired
    private IUserTaglibrarySV userTaglibrarySV;

    @Override
    public boolean insertEvalDoctor(EvalDoctorParam param) {
        // 1. 保存评价记录
        CommentRecordEntity recordEntity = selectOne(TK
                .select(CommentRecordEntity.class)
                .where()
                .andEqualTo(CommentRecordEntity::getOrderId, param.getOrderId())
                .end());
        if (recordEntity != null) {
            throw new BusinessException("已经评价了喔");
        }
        insert(param);

        //2.关联标签
        List<PatientRateTagVo> patientRateTagVos = param.getPatientRateTagVos();
        for (PatientRateTagVo model : patientRateTagVos) {
            userTaglibrarySV.insert(
                    UserTaglibraryEntity
                            .builder()
                            .orderId(param.getOrderId())
                            .tagedUserId(param.getCommentAppperson())
                            .tagedUserId(param.getCommentPerson())
                            .tagId(model.getId())
                            .tagType(model.getTglbType())
                            .build());
        }


        return false;
    }

    /**
     * 患者评价
     * @param currId
     * @param ioId
     * @return
     */
    @Override
    public PhoneOrderCommentRecord selectComment(Long currId, Long ioId) {
        return mapper.selectComment(currId,ioId);
    }


    @Override
    public List<LabelsEntity> hotLabels(Long doctorId) {
        List<LabelsEntity> labels = mapper.hotLabels(doctorId);
        LabelsEntity labels1 = new LabelsEntity();
        long count = mapper.hotLabelsCount(doctorId);
        labels1.setDlType("全部");
        labels1.setDlValue(String.valueOf(count));
        labels.add(labels1);
        return labels;
    }

    @Override
    public List<HotPointVo> hotLabelsOrders(Long doctorId, String labels) {
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotBlank(labels) && !"全部".equals(labels)) {
            String[] labelsArray = labels.split(",");
            //组装sql
            for (int i = 0; i < labelsArray.length; i++) {
                sb.append(" tab.possDisease like concat(\"%\",\"" + labelsArray[i] + "\",\"%\") ");
                if (i != labelsArray.length - 1) {
                    sb.append(" or ");
                }
            }
        }
        List<HotPointVo> hotPointVoList = mapper.hotLabelsOrders(doctorId, sb.toString());
        return hotPointVoList;
    }

    @Override
    public List<HotPointVo> selectCommentRecords(String okay, Long doctId, Long[] tagStand, Long[] tagCustom) {
        return mapper.selectCommentRecords(okay, doctId, tagStand, tagCustom);
    }
}
